<template>
  <div>
    <!-- 按钮 -->

    <el-button style="margin:10px 10px" type="primary" icon="el-icon-refresh-right" circle
      @click="getDynamicExtensionProcessList"></el-button>
    <el-button type="primary" @click="add()">添加</el-button>

    <el-table style="width: 100%;" border :data="list">
     <el-table-column prop="giveGold" label="金币" align="center">
      </el-table-column>
      <el-table-column prop="giveMoreGold" label="金币(额外赠送)" align="center">
      </el-table-column>
      <el-table-column prop="sumGold" label="总计可获得金币" align="center">
      </el-table-column>
      <el-table-column prop="rechargeMoney" label="充值金额(元)" align="center">
      </el-table-column>
      <!--      <el-table-column prop="isCustom" label="是否自定义" align="center">
        <template slot-scope="{ row, $index }">
          <div>{{ row.isCustom == "0" ? '否' : '是' }}</div>
        </template>
      </el-table-column> -->

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{ row, $index }">
          <el-tooltip content="修改" placement="top" slot="reference">
            <el-button type="primary" icon="el-icon-edit" @click="ubpateBannar(row)"
              :disabled="!list.isCustom ? false : true"></el-button>
          </el-tooltip>
          <el-popconfirm title="是否删除？" icon="el-icon-info" icon-color="red" @confirm="getDelete(row.id)">
            <el-tooltip content="删除" placement="top" slot="reference">
              <el-button type="danger" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 对话框  -->
    <el-dialog title="修改配置" :visible.sync="dialogFormVisible">
      <el-form style="width:80%" :model="tmFrom" ref="ruleForm">
   <!--     <el-form-item label="金币" label-width="100px" prop="giveGold">
          <el-input autocomplete="off" v-model="tmFrom.giveGold"></el-input>
        </el-form-item> -->
        <el-form-item label="金币(赠送)" label-width="100px" prop="giveMoreGold">
          <el-input autocomplete="off" v-model="tmFrom.giveMoreGold"></el-input>
        </el-form-item>
        <el-form-item label="充值金额(元)" label-width="100px" prop="rechargeMoney">
          <el-input autocomplete="off" v-model="tmFrom.rechargeMoney"></el-input>
        </el-form-item>
<!--        <el-form-item label="预计时间/分钟" label-width="100px" prop="estimatedTime">
          <el-input autocomplete="off" v-model="tmFrom.estimatedTime"></el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="UpdateBillingRules">修改</el-button>
      </div>
    </el-dialog>

    <!-- 添加对话框  -->
    <el-dialog title="添加配置" :visible.sync="dialogFormVisible1">
      <el-form style="width:80%" :model="list1" :rules="rules1" ref="ruleForm">
      <!-- <el-form-item label="金币" label-width="100px" prop="giveGold">
         <el-input autocomplete="off" v-model="list1.giveGold"></el-input>
       </el-form-item> -->
       <el-form-item label="金币(赠送)" label-width="100px" prop="giveMoreGold">
         <el-input autocomplete="off" v-model="list1.giveMoreGold"></el-input>
       </el-form-item>
       <el-form-item label="充值金额(元)" label-width="100px" prop="rechargeMoney">
         <el-input autocomplete="off" v-model="list1.rechargeMoney"></el-input>
       </el-form-item>
<!--        <el-form-item label="预计时间/分钟" label-width="100px" prop="estimatedTime">
          <el-input autocomplete="off" v-model="list1.estimatedTime"></el-input>
        </el-form-item>
        <el-form-item label="是否自定义">
          <el-select v-model="list1.isCustom" placeholder="请选择是否自定义" prop="isCustom">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="getAddDynamicExtensionConfiguration">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'RechargeMoney',
    data() {

      return {
        // 修改时的数据
        //列表数据
        list: [],
        // 添加列表
        list1: {
          // // 金币花费
          // costGold: "",
          // // 预计时间
          // estimatedTime: "",
          // // 名称
          // intro: "",
          // // 是否自定义
          // isCustom: "",
          // // 预计播放量
          // playNum: ""
        },

        // 对话框显示与隐藏控制
        dialogFormVisible: false,
        dialogFormVisible1: false,
        // 表单对象
        tmFrom: {},
        // 当前为修改还是添加 修改为false 添加为true
        state: true,
        // 表单验证
        rules1:{}
        // rules1: {
        //   intro: [{
        //       required: true,
        //       message: '请输入活动名称',
        //       trigger: 'chang'
        //     },
        //     {
        //       min: 3,
        //       max: 5,
        //       message: '长度在 3 到 5 个字符',
        //       trigger: 'blur'
        //     }
        //   ],
        //   costGold: [{
        //       required: true,
        //       message: '请输入金币花费',
        //       trigger: 'int'
        //     },
        //     // { min: 3, max: 5, message: '请输入金币花费', trigger: 'blur' }
        //   ],
        //   playNum: [{
        //       required: true,
        //       message: '请输入预计播放量',
        //       trigger: 'int'
        //     },
        //     // { min: 1, max: 99999, message: '请输入预计播放量', trigger: 'int' }
        //   ],
        //   estimatedTime: [{
        //       required: true,
        //       message: '请输入预计时间',
        //       trigger: 'change'
        //     },
        //     // { min: 1, max: 9999, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        //   ]
        // },
      }
    },
    mounted() {
      this.getRechargeMoneyList()
    },

    methods: {
      // 获取推广套餐列表
      async getRechargeMoneyList() {
        let res = await this.$API.system.getRechargeMoneyList()
        console.log(res, "108")
        this.list = res.data;
      },
      // 刷新
      getDynamicExtensionProcessList() {
        this.getRechargeMoneyList()
      },
      // 添加一条数据窗口弹出
      add() {
        // this.state = true
        this.dialogFormVisible1 = true
        this.list1 = {
          // // 金币花费
          // costGold: "",
          // // 预计时间
          // estimatedTime: "",
          // // 名称
          // intro: "",
          // // 是否自定义
          // isCustom: "",
          // // 预计播放量
          // playNum: ""
        }
      },
      // 添加一条数据发送到后端
      getAddDynamicExtensionConfiguration() {
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            let res = await this.$API.system.addRechargeMoney(this.list1);
            console.log(res);
            this.dialogFormVisible1 = false;
            this.getRechargeMoneyList()
          } else {
            console.log('error submit!!');
            return false;
          }
        })

        // console.log(this.tmFrom);
      },
      // 点击修改
      ubpateBannar(row) {
        this.state = false
        this.dialogFormVisible = true;
        // 浅拷贝  让tmFrom收到的数据非服务器直接传回的数据
        this.tmFrom = {
          ...row
        };
        console.log(this.tmFrom);
      },
      // 点击修改
      UpdateBillingRules() {
        // console.log(this.id);
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            let res = await this.$API.system.updateRechargeMoney(this.tmFrom);
            this.dialogFormVisible = false;
            this.getRechargeMoneyList()
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      },
      // 删除
      async getDelete(id) {
        let res = await this.$API.system.deleteRechargeMoney({
          id: id
        });
        console.log(res);
        this.getRechargeMoneyList()
      }
    }
  }
</script>

<style lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
